using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace PFDB
{
	public partial class QueryRunner : Form
	{
		string server;
		string DBName;
		string username;
		string password;
		MySqlConnection conn;
		public QueryRunner()
		{
			InitializeComponent();
		}

		public QueryRunner(string DBName, string TextBoxlINES, string username, string password, string server)
		{
			this.server = server;
			this.DBName = DBName;
			this.username = username;
			this.password = password;
			InitializeComponent();
			myRichTextBox1.Rtf = TextBoxlINES;
			UpdateFormData();
			//myRichTextBox1.ProcessAllLines();
		}
		private void UpdateFormData()
		{
			string line = String.Empty;
			int i = 0;
			try
			{
				while ((line = myRichTextBox1.Lines[i]).Length < 7)
				{
					i++;
				}
			} catch
			{
				line = "Query Runner - Empty";
			}
			this.Text = "Query Runner - " + line;
			//this.Refresh();
		}
		private void button1_Click(object sender, EventArgs e)
		{
			conn = AppLib.MYSQLSET.DBConnect.conn(DBName, username, password, server);
			conn.Open();
			MySqlCommand comm = new MySqlCommand(myRichTextBox1.Text, conn);
			MySqlDataAdapter adapter = new MySqlDataAdapter(comm);
			DataTable table = new DataTable();
			table.Locale = System.Globalization.CultureInfo.InvariantCulture;
			try
			{
				adapter.Fill(table);
				bindingSource1.DataSource = table;
				// Resize the DataGridView columns to fit the newly loaded content.
				dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
				// you can make it grid readonly.
				dataGridView1.ReadOnly = true;
				// finally bind the data to the grid
				dataGridView1.DataSource = bindingSource1;
				laStatus.Text = "Query Success!";
			} catch (Exception exc)
			{
				laStatus.Text = exc.Message;
			}
			UpdateFormData();
		}

		private void button1_KeyPress(object sender, KeyPressEventArgs e)
		{
			myRichTextBox1.Text = e.KeyChar.ToString();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			myRichTextBox1.ProcessAllLines();
			myRichTextBox1.Refresh();
		}

		private void myRichTextBox1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F9)
			{
				button1_Click(myRichTextBox1, new EventArgs());
			}
			if (e.KeyCode == Keys.F8)
			{
				button2_Click(myRichTextBox1, new EventArgs());
			}
			if (e.KeyCode == Keys.F2)
			{
				splitContainer1.Panel2Collapsed = !splitContainer1.Panel2Collapsed;
			}
			if (e.KeyCode == Keys.F1)
			{
				splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
			}
		}

		private void QueryRunner_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F9)
			{
				button1_Click(myRichTextBox1, new EventArgs());
			}
			if (e.KeyCode == Keys.F8)
			{
				button2_Click(myRichTextBox1, new EventArgs());
			}
			if (e.KeyCode == Keys.F2)
			{
				splitContainer1.Panel2Collapsed = !splitContainer1.Panel2Collapsed;
			}
			if (e.KeyCode == Keys.F1)
			{
				splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
			}
		}

		private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			myRichTextBox1.AppendText(((DataGridView)sender)[e.ColumnIndex, e.RowIndex].Value.ToString());
			myRichTextBox1.AppendText(", \n");
		}

		private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				DataGridView tempDGView = (DataGridView)sender;
				for (int i = tempDGView.SelectedCells.Count - 1; i >= 0; i--)
				{
					myRichTextBox1.AppendText(tempDGView.SelectedCells[i].Value.ToString() + ", \n");
				}
			}
			if (e.Button == MouseButtons.Middle)
			{
				DataGridView tempDGView = (DataGridView)sender;
				for (int i = tempDGView.SelectedCells.Count - 1; i >= 0; i--)
				{
					myRichTextBox1.AppendText(tempDGView.SelectedCells[i].Value.ToString() + ", ");
				}
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			if (saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				File.WriteAllLines(saveFileDialog1.FileName, myRichTextBox1.Lines, Encoding.ASCII);
			}
		}

		private void splitContainer1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			splitContainer1.Panel2Collapsed = true;
		}
	}
}
